package io.renren.modules.basedata.service.impl;

import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.IssuerUploadRequest;
import io.renren.common.utils.CreateFileUtil;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.IssuerUploadDao;
import io.renren.modules.basedata.entity.tbl_issuer;
import io.renren.modules.basedata.service.IssuerUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class IssuerUploadServiceImpl implements IssuerUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    IssuerUploadDao issuerUploadDao;

    public String issuerUpload() throws Exception {
        log.info("开始获取发行方信息！");
        int operResult = 0;
        List<tbl_issuer> list = null;
        tbl_issuer issuer = null;
        list = this.getIssuerList();
        if (list != null && list.size() > 0) {
            for(int i = 0; i < list.size(); ++i) {
                issuer = (tbl_issuer)list.get(i);
                IssuerUploadRequest request = new IssuerUploadRequest();
                request.setId(issuer.getId());
                request.setName(issuer.getName());
                request.setTel(issuer.getTel());
                request.setContact(issuer.getContact());
                request.setAddress(issuer.getAddress());
                request.setStartTime(DateUtil.formatDate(issuer.getStartTime()));
                request.setEndTime(DateUtil.formatDate(issuer.getEndTime()));
                request.setOperation(issuer.getFlag());
                String fileName = ETCCommHelper.getFileName(request, "65");
                //生成json文件
				CreateFileUtil.createJsonFile(request.toJsonStr(), "",fileName);
              /*  try {
                    IssuerUploadResponse response = (IssuerUploadResponse)ETCCommHelper.sdtsUpload(fileName, request, IssuerUploadResponse.class);
                    log.info("上传发行方信息服务器返回：" + response.toJson());
                    if (response != null && "成功！".equals(response.getInfo())) {
                        if (issuerUploadDao.updateIssuerUploadflag(issuer.getId()) == 1) {
                            log.info("更新发行方数据上传标志成功！");
                        } else {
                            log.info("更新发行方数据上传标志失败！");
                        }
                    } else {
                        log.info("上传发行方信息失败!失败原因：" + response.getInfo());
                        issuerUploadDao.updateIssuerUploadflagError(issuer.getId());
                    }
                } catch (IOException var9) {
                    log.error("上传发行方信息失败！错误信息：",var9);
                    var9.printStackTrace();
                } catch (ApiRequestException var10) {
                    log.error("上传发行方信息失败！错误信息：",var10);
                    if (var10.getErrCode() >= 700 && var10.getErrCode() <= 800) {
                        if (var10.getErrCode() == 707) {
                            issuerUploadDao.updateIssuerUploadflag(issuer.getId());
                        } else {
                            issuerUploadDao.updateIssuerUploadflagError(issuer.getId());
                        }
                    }

                    var10.printStackTrace();
                }*/
            }

            return "上传发行方信息结束！";
        } else {
            return "无发行方信息,上传结束...";
        }
    }

    public List<tbl_issuer> getIssuerList() throws IOException {
        List<tbl_issuer> issuerList = null;
        try {
            issuerList = issuerUploadDao.getIssuerList();

        } catch (Exception var3) {
            var3.printStackTrace();
        }
        return issuerList;
    }

}
